import common.TreeNode;

/**
 * @author tmh
 * @date 2024/10/7 17:34
 * @description
 */
public class T437路径总和III {
    private int count;
    private int targetSum;

    public int pathSum(TreeNode root, int targetSum) {
        this.targetSum=targetSum;
        dfsTree(root);
        return count;

    }

    private void dfsTree(TreeNode root){
        if (root==null){
            return;
        }
        dfs(root, root.val);
        dfsTree(root.left);
        dfsTree(root.right);
    }
    //注意测试数据的范围，这里是long，int的最大值是21亿
    private void dfs(TreeNode root, long sum) {
        if (sum == targetSum) {
            count++;
        }
        if (root.left!=null){
            dfs(root.left,sum+root.left.val);
        }
        if (root.right!=null){
            dfs(root.right,sum+root.right.val);
        }
    }
}
